package com.ilovepostcard.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.ilovepostcard.DiscoverActivity;
import com.ilovepostcard.LaunchCreateActivity;
import com.ilovepostcard.MyActivity;
import com.ilovepostcard.R;
import com.ilovepostcard.utils.Utils;

public class PostcardBottomBar extends LinearLayout {

    private static final String VIEW_DISCOVER = "discover";
    private static final String VIEW_CREATE = "create";
    private static final String VIEW_MY = "my";

    View menuDiscover;
    View menuCreate;
    View menuMy;
    
    private String currentView;

    public PostcardBottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PostcardBottomMenu);
        currentView = ta.getString(R.styleable.PostcardBottomMenu_currentView);
        inflate();
    }

    public PostcardBottomBar(Context context) {
        super(context);
        inflate();
    }
    
    public void setView(String view) {
        currentView = view;
        setupStatus();
    }

    private void setupStatus() {
        menuDiscover.setBackgroundResource(R.drawable.discover_bottonbar_icon_press);
        menuCreate.setBackgroundResource(R.drawable.discover_bottonbar_icon_press);
        menuMy.setBackgroundResource(R.drawable.discover_bottonbar_icon_press);
        if (!VIEW_DISCOVER.equals(currentView)) {
            menuDiscover.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((Activity) getContext()).finish();
                    Intent intent = new Intent(getContext(), DiscoverActivity.class);
                    getContext().startActivity(intent);
                }
            });
            menuDiscover.setBackgroundResource(R.drawable.bottom_menu_bg_selector);
        }
        if (!VIEW_CREATE.equals(currentView)) {
            menuCreate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Utils.showChooseDialog(getContext(), new String[] {"相机", "从相册中选"}, new Runnable[] {
                        new Runnable() {
                            public void run() {
                                Intent intent = new Intent(getContext(), LaunchCreateActivity.class);
                                intent.putExtra(LaunchCreateActivity.LAUNCH_TYPE_KEY, LaunchCreateActivity.LAUNCH_TYPE_CAMERA);
                                getContext().startActivity(intent);
                            }
                        },
                        new Runnable() {
                            public void run() {
                                Intent intent = new Intent(getContext(), LaunchCreateActivity.class);
                                intent.putExtra(LaunchCreateActivity.LAUNCH_TYPE_KEY, LaunchCreateActivity.LAUNCH_TYPE_GALLERY);
                                getContext().startActivity(intent);
                            }
                        }
                    });
                }
            });
            menuCreate.setBackgroundResource(R.drawable.bottom_menu_bg_selector);
        }
        if (!VIEW_MY.equals(currentView)) {
            menuMy.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((Activity) getContext()).finish();
                    Intent intent = new Intent(getContext(), MyActivity.class);
                    getContext().startActivity(intent);
                }
            });
            menuMy.setBackgroundResource(R.drawable.bottom_menu_bg_selector);
        }
    }
    
    private void inflate() {
        View view = View.inflate(getContext(), R.layout.postcard_bottom_menu, this);
        menuDiscover = view.findViewById(R.id.bottom_menu_discover);
        menuCreate = view.findViewById(R.id.bottom_menu_create);
        menuMy = view.findViewById(R.id.bottom_menu_my);
        setupStatus();
    }
}
